﻿namespace com.stifel.ServerBuildMgmt
{
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelVLAN_Definition : BaseDefinition
    {
        #region Fields

        const string SITECOLUMNS = "Stifel-VLAN.xml";

        #endregion Fields

        #region Constructors

        public StifelVLAN_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelVLAN_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            loadList( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        int getClusterId( string value )
        {
            StifelVLANClusterDao dao = new StifelVLANClusterDao( );
            StifelVLANCluster o = dao.GetByTitle( value );
            return o == null ? 0 : o.Id.Value;
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            StifelVLAN c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelVLAN>( ),
                ContentTypeId = Item.GetContentTypeId<StifelVLAN>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes a VLAN.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVLAN>(c.GetPropertySymbol(o=>o.VLANId)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVLAN>(c.GetPropertySymbol(o=>o.VLANIp)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVLAN>(c.GetPropertySymbol(o=>o.LocationId)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVLAN>(c.GetPropertySymbol(o=>o.ClusterId)),
                        Required=true
                    },
                }.Distinct( ).ToList( )
            };
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelVLANDao.ListName,
                Title = "Stifel VLAN",
                Description = "Contains the VLANs.",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<StifelVLAN>()
                }.ToList( )
            };
        }

        int getLocationId( string value )
        {
            StifelVirtualServerLocationDao dao = new StifelVirtualServerLocationDao( );
            StifelVirtualServerLocation o = dao.GetByTitle( value );
            return o == null ? 0 : o.Id.Value;
        }

        void loadList( )
        {
            StifelVLANDao dao = new StifelVLANDao( );
            dao.Clear( );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "St Louis" ),
                ClusterId = getClusterId( "HA1" ),
                Title = "SQL",
                VLANId = 700,
                VLANIp = "172.22.32.0/25"
            } );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "St Louis" ),
                ClusterId = getClusterId( "DMZ" ),
                Title = "200",
                VLANId = 200,
                VLANIp = ""
            } );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "St Louis" ),
                ClusterId = getClusterId( "SRM" ),
                Title = "Extended SQL",
                VLANId = 1205,
                VLANIp = "172.24.0.0/25"
            } );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "Baltimore" ),
                ClusterId = getClusterId( "" ),
                Title = "Apps",
                VLANId = 226,
                VLANIp = "172.27.153.0/24"
            } );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "San Francisco" ),
                ClusterId = getClusterId( "HA1" ),
                Title = "Tweisel Servers",
                VLANId = 10,
                VLANIp = ""
            } );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "Jersey City" ),
                ClusterId = getClusterId( "DMZ" ),
                Title = "200",
                VLANId = 200,
                VLANIp = ""
            } );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "Jersey City" ),
                ClusterId = getClusterId( "SRM" ),
                Title = "EXT SQL",
                VLANId = 1205,
                VLANIp = "172.24.0.0/25"
            } );
            dao.Add( new StifelVLAN
            {
                LocationId = getLocationId( "Jersey City" ),
                ClusterId = getClusterId( "HA1" ),
                Title = "SQL",
                VLANId = 700,
                VLANIp = "172.23.32.0/25"
            } );
        }

        #endregion Methods
    }
}